<?php
include_once 'app/modul/db/DBGuestbook.php';

class guestbook implements UserModul{
    
    private $daten;
    private $newpost = "modul/guestbook/new";
    private $limit = 5;
    private $insertTimeBlock = 15;
    private $position = 0;
    public $error = false;
    private $visitorID = NULL;
    private $ownerID = NULL;
    
	public function __construct($design = NULL){
	    $this->daten = $design;	
	    
	    if(isset($_SESSION['UserId'])){
	        $this->visitorID = $_SESSION['UserId'];	
	    }
	}
//	public function __autoload(){
//	    $this->getSetting($_SESSION['UserId']);
//	}
    public function Main(){	
        $this->getSetting($this->daten->User_Id);
        
        $this->daten->design->assign("paginator_aktiv",$this->position);
        $this->daten->design->assign("paginator",$this->getPaginator($this->daten->User_Id));
        $this->daten->design->assign("modul",DBGuestbook::getGuestBook($this->daten->User_Id,$this->position,$this->limit));
	}
    public function Widget(){

    }
	public function getRequest($design, $page){
	    
	    if(isset($design->get->del)){
	        DBGuestbook::delComment($design->var['visitorID'],$design->get->del);
	    }
	    if(isset($design->get->new)){
	        $page->underpage = 999999;
	        $page->link = $this->newpost;
	    }
	    if(isset($design->get->page)){
	        $this->position = $design->get->page;
	    }
	    
	    if(isset($design->post->newpostTitel) && isset($design->post->newpostText)){
	        $this->getSetting();
	        if(DBGuestbook::checkInsertBlock($page->user_id,$this->insertTimeBlock)){	        
	            DBGuestbook::setPost($page->user_id,$design->post->newpostTitel,$design->post->newpostText,$this->visitorID,$design->post->newpostFromName,$design->post->newpostFromEmail);
	        }else{	            
	            $this->error = "Nanu das war zu schnell, erst wieder in $this->insertTimeBlock Minuten eintrag möglich";
	        }	        
	    }
	    
	    return $page;	    
	}
	public function getPaginator($userid){
	    $counter = 0;
	    $pagiLimit = 3;
	    $tmp = 999;
	    $final = array();
	    $total = DBGuestbook::getCount($userid)-1;
	    $totalPages = $total / $this->limit;
	    
	    if(0 < $this->position){
	        for ($y = $this->position;0 < $y ;$y = $y - 1){
	           $counter++;
	           array_push($final, $y);
	           if($counter == $pagiLimit){
	               break;
	           }
	        }	       
	    }
	    $counter = 0; 	    
	    if($totalPages < $this->position){
	        for ($x = $this->position;$x > 1;$x--){
	           $counter++;
	           array_push($final, $x);
	           if($counter == $pagiLimit){
	               break;
	           }
	        }
	    }
	    $counter = 0;
	    
	    if($this->position == 0){
	        $this->position = 1;
	        $tmp = 0;
	    }
	    
	    if($totalPages > $this->position){
	        for ($t = $this->position;$totalPages >=$t;$t++){
	           $counter++;
	           array_push($final, $t);
	           if($counter == $pagiLimit){
	               break;
	           }
	        }
	    }
	    
	    if($tmp == 0){	        
	        $this->position = 0;
	    }	
	        $final = array_flip($final);
	    ksort($final);
	    
	    foreach ($final as $key => $pagi){
	        $final[$key] = "?page=".$key;
	    }
	    return $final;	   
    }
    public function getSetting(){
        $DbSetting = DBGuestbook::getSetting($this->daten->User_Id);
        $this->limit = $DbSetting->limit;
        $this->insertTimeBlock = $DbSetting->insertTimeBlock;
    }
    public function getDbSetting(){
        $DbSetting = DBGuestbook::getSetting($this->daten->var["UserData"]->id);
        return $DbSetting;
    }
    public function updateDbSetting($user_id,$value){
        $DbSetting = DBGuestbook::updateDBSetting($user_id,$value);
        return $DbSetting;
    }
    public function setSetting(){
        if(DBGuestbook::setDbSetting($this->daten->var["UserData"]->id)){        
            $value = array();
            $value['name'] = "Gaestebuch";	  
            $value['slogen'] = "hinter lass eine Nachricht";
            $value['titel'] = "Mein Gästebuch";
            $value['url'] = "gaestebuch.html";	  
            $value['aktiv'] = 0;	
            $value['zugriff'] = 0;		
            $value['navi'] = 0;	        
            $value['navi_posi'] = 1;
            $value['main_flag'] = 0;

            return $value;
        }else{
            return false;
        }
    }
/*    CREATE TABLE `pagemaker`.`guestbook_setting` (
    `id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `user_id` INT( 10 ) NOT NULL ,
    `insertTimeBlock` INT( 3 ) NOT NULL DEFAULT '15',
    `limit` INT( 2 ) NOT NULL DEFAULT '5'
    ) ENGINE = InnoDB;*/
}
?>